package chapter08;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.locks.ReentrantLock;

@Slf4j
public class ReentrantLockTest {
    public static void main(String[] args) throws InterruptedException {
        ReentrantLock reentrantLock = new ReentrantLock();

        Thread thread1 = new Thread(() -> {
            try {
                reentrantLock.lockInterruptibly();
                log.debug("线程开始执行...");
                Thread.sleep(5000);
            }catch (Exception e){
                log.debug("被打断了");
            }finally {
                reentrantLock.unlock();
            }
        },"t1");

        Thread thread2 = new Thread(() -> {
            try {
                reentrantLock.lock();
                log.debug("线程开始执行...");
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                log.debug("被打断了");
            }finally {
                reentrantLock.unlock();
            }
        },"t2");



        thread2.start();

        Thread.sleep(1000);
        thread1.start();
        Thread.sleep(2000);
        thread1.interrupt();
//        thread2.interrupt();

    }
}
